<?php

namespace App\Http\Controllers\Activity;

use App\Http\Controllers\Controller;
use App\Models\Activity;
use App\Models\Goods;
use App\Models\Shop;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;

class ActivityDetailController extends Controller
{

    /**
     * 查询霸王餐活动详情
     */
    public function index()
    {

        $all = $this->all;
        $act_id = $all['act_id'];
        // 活动信息
        $activity_message = Activity::activityMessage($act_id);
        // 店铺信息
        $shop_message = Shop::shopMessage($activity_message['shop_id']);
        // 奖品信息
        $goods_message = Goods::goodsMessage($activity_message['goods_id']);
        // 轮转开奖人:正在开奖时的信息
        if ($activity_message['status'] === Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_PRE) {
            $winning_message = DB::table('lottery_users_draw as lud')
                ->leftJoin('talent', 'lud.we_id', '=', 'talent.we_id')
                ->leftJoin('explore_channel as ec', 'talent.channel_id', '=', 'ec.channel_id')
                ->where('lud.act_id', '=', $act_id)
                ->where('lud.status', '=', Data::DATA_DB_LOTTERY_USERS_DRAW_STATUS_WINING)
                ->select('lud.win_at', 'lud.cancel_at', 'talent.talent_id', 'ec.channel_name', 'talent.account_name')
                ->get()
                ->toArray();
            $winning_message = json_decode(json_encode($winning_message), true);
            $winning_message = $winning_message[0];
        }
        // 中奖信息
        $winner = in_array($activity_message['status'], [
            Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_AFTER,
            Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_SUCCESS,
        ]);
        $winner_message = $winner ? Activity::winnerMessage($act_id) : null;
        // 参与信息
        $participate_message = Activity::participateMessage($act_id);

        return $this->setReturnJson(0, 'success', [
            'activity' => $activity_message,
            'shop' => $shop_message,
            'goods' => $goods_message,
            'winner' => $winner_message,
            'winning' => $winning_message ?? null,
            'participate' => $participate_message,
        ]);

    }

}
